// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A [author] rhyme_lph
// p [email]  rhymelph@gmail.com
// L [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/employee_contact_entity.dart';
import 'package:finance_app/src/entities/leave_item_entity.dart';
import 'package:finance_app/src/http_service/http_leave_service.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/utils/form_utils.dart';
import 'package:flutter/material.dart';
import 'package:dio/src/response.dart';
import 'dart:async';

class LeaveDetailsProvider extends BaseProvider<List<LeaveItemEntity>> {
  EmployeeContactEntity currentSelectEmployee;
  List<EmployeeContactEntity> employees;
  String startDate, endDate;

  ScrollController controller = ScrollController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Future<Response> get dataFuture async {
    List<int> ids = employees.map((e) => e.id).toList();
    return await HttpLeaveService.leavesListByDateRange(
        currentSelectEmployee != null ? [currentSelectEmployee.id] : ids,
        'approved',
        null,
        startDate,
        endDate);
  }

  void onChangeEmployee() async {
    final result = await Routes.navigateTo(
        RouterGenProviders.employeeContactsPage,
        params: {
          'select': currentSelectEmployee,
          'canAdd': false,
          'showAll': true,
          'employees': employees
        });
    if (result != null && result is EmployeeContactEntity) {
      currentSelectEmployee = result;
      if (result.id == -1) {
        currentSelectEmployee = null;
      }
      update();
      obtainContext?.call((context) async {
        BaseConfig.dialogBase
            .showLoadingDialog(context: context, text: S.current.loading);
        await _loadLeaves();
        Navigator.of(context).pop();
      });
    }
  }

  @override
  FutureOr<List<LeaveItemEntity>> transformData(
      List<LeaveItemEntity> d, bool isCache) async {
    return super.transformData(d, isCache);
  }

  void onNavDetail(LeaveItemEntity e) async {
    final result =
        await Routes.navigateTo(RouterGenProviders.newLeavePage, params: {
      'data': e,
    });
    if (result != null && result is bool) {
      data.remove(e);
      update();
    }
  }

  String getLeaveStatus(LeaveItemEntity e) {
    if (e.status == 'approved') {
      final dateTime = DateTime.now();
      final startTime = DateTimeUtils.toDateTime(e.startDate, 'yyyy-MM-dd');
      return startTime.isBefore(dateTime) ? null : 'Upcoming';
    }
    return FormUtils.getStatus(ReimburseModel.leave, e.status, 0);
  }

  Future<void> _loadLeaves() async {
    await post(dataFuture, autoHandler: AutoHandler.Toast, onSuccess: (result) {
      data = getEntityFromResult<List<LeaveItemEntity>>(result);
      update();
    });
  }
}
